﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using Microsoft.Win32;

namespace GEPSDevelopEnvironment.Common
{
    class SVNClientAdapter
    {
        public static void ExecSVN(emSVNOperater AOperater, string APath, out string AMessage, string AParameter = "")
        {
            AMessage = "";
            switch (AOperater)
            {
                case emSVNOperater.svnAdd:
                    Add(APath, out AMessage);
                    break;
                case emSVNOperater.svnCommit:
                    Commit(APath, AParameter, out AMessage);
                    break;
                case emSVNOperater.svnDelete:
                    Delete(APath, out AMessage);
                    break;
                case emSVNOperater.svnDiff:
                    Diff(APath, out AMessage);
                    break;
                case emSVNOperater.svnLock:
                    Lock(APath, AParameter, out AMessage);
                    break;
                case emSVNOperater.svnUpdate:
                    Update(APath, out AMessage);
                    break;
                case emSVNOperater.svnRevert:
                    Revert(APath, out AMessage);
                    break;
                case emSVNOperater.svnShowLog:
                    ShowLog(APath, out AMessage);
                    break;
                case emSVNOperater.svnUnlock:
                    UnLock(APath, out AMessage);
                    break;
                case emSVNOperater.svnInfo:
                    Info(APath, out AMessage);
                    break;
            }
        }

        public static bool Add(string PathName, out string AMessage)
        {
            return ExecuteCommand(string.Format("add {0}", PathName), out AMessage);
        }

        public static bool Cleanup(string PathName, out string AMessage)
        {
            return ExecuteCommand(string.Format("cleanup {0}", PathName), out AMessage);
        }

        public static bool Commit(string PathName, string LogAMessage, out string AMessage)
        {
            return ExecuteCommand(string.Format("commit {0} /logmsg:{1}", PathName, LogAMessage), out AMessage);
        }

        public static bool CheckOut(string URL, string PathName, out string AMessage)
        {
            return ExecuteCommand(string.Format("checkout -r HEAD -q --force \"{0}\" \"{1}\"", URL, PathName), out AMessage);
        }

        public static bool Delete(string PathName, out string AMessage)
        {
            return ExecuteCommand(string.Format("remove {0}", PathName), out AMessage);
        }

        public static bool Diff(string PathName, out string AMessage)
        {
            return ExecuteCommand(string.Format("diff {0}", PathName), out AMessage);
        }

        public static string DosQuotedStr(string S)
        {
            return "\"" + S + "\"";
        }

        ////////////////////////////////////////////////////////////////////////////////
        //设计：Linc 2008.10.05
        //功能：执行命令
        //参数：
        //注意：字符串会自动添加 “”
        ////////////////////////////////////////////////////////////////////////////////
        public static bool ExecuteCommand(string AFmt, out string AMessage)
        {
            string sTortoisePath  = TortoiseSVNClientAdapter.InitTortoise();
            if (!File.Exists(sTortoisePath))
            {
                AMessage = "没有安装TortoiseSVN！";
                return false;
            }
            int nExitCode;
            AMessage = ProcessCommonUtils.GetCommandLineOutput(AFmt, out nExitCode, 
                DosQuotedStr(Path.GetDirectoryName(sTortoisePath) + "\\SVN.exe"));
            return nExitCode == 0;
        }

        public static bool Lock(string PathName, string LogAMessage, out string AMessage)
        {
            return ExecuteCommand(string.Format("lock {0} /logmsg:{1}", PathName, LogAMessage), out AMessage);
        }

        public static bool Info(string PathName, out string AMessage)
        {
            return ExecuteCommand(string.Format("info {0}", PathName), out AMessage);
        }


        public static bool IsVersioned(string PathName, out string AMessage)
        {
            AMessage = "";
            return true;
        }

        public static bool Revert(string PathName, out string AMessage)
        {
            return ExecuteCommand(string.Format("revert {0}", PathName), out AMessage);
        }

        public static bool ShowLog(string PathName, out string AMessage)
        {
            return ExecuteCommand(string.Format("log {0}", PathName), out AMessage);
        }

        public static bool UnLock(string PathName, out string AMessage)
        {
            return ExecuteCommand(string.Format("unlock {0}", PathName), out AMessage);
        }

        public static bool Update(string PathName, out string AMessage)
        {
            return ExecuteCommand(string.Format("update {0}", PathName), out AMessage);
        }

        public static bool Browse(string PathName, out string AMessage)
        {
            return ExecuteCommand(string.Format("repobrowser {0}", PathName), out AMessage);
        }
    }
}
